package io.geekidea.boot.util.baiwangApi;

import com.baiwang.open.client.BWRestClient;
import com.baiwang.open.client.IBWClient;
import com.baiwang.open.client.login.BopLoginClient;
import com.baiwang.open.client.login.BopLoginResponse;
import com.baiwang.open.client.login.PasswordLoginClient;
import com.baiwang.open.client.login.PasswordLoginConfig;
import com.baiwang.open.entity.request.SOutputinvoicePushRequest;
import com.baiwang.open.entity.response.SOutputinvoicePushResponse;
import com.baiwang.open.exception.BWOpenException;
import io.geekidea.boot.config.baiwang.BaiwangConfig;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 交付发票
 */
@Component
public class Push {

    static String url = BaiwangConfig.getUrl();
    static String appKey = BaiwangConfig.getAppKey();
    static String userName = BaiwangConfig.getUserName();
    static String password = BaiwangConfig.getPassword();
    static String appSecret = BaiwangConfig.getAppSecret();
    static String userSalt = BaiwangConfig.getUserSalt();

    public static SOutputinvoicePushResponse pushInvoice(
            String taxNo,//必填 销方税号
            String invoiceCode,//非必填-发票代码
            String invoiceNo,//非必填-发票号码
            String digitInvoiceNo,//非必填-数电发票号码
            String pushPhone, //非必填-交付手机交付手机
            String pushEmail//非必填-交付邮箱

    ) throws BWOpenException {
        try {
            String requestId = UUID.randomUUID().toString(); //自定义请求唯一标识
            PasswordLoginConfig loginConfig = new PasswordLoginConfig();
            loginConfig.setUrl(url);
            loginConfig.setClientId(appKey);
            loginConfig.setClientSecret(appSecret);
            loginConfig.setUsername(userName);
            loginConfig.setPassword(password);
            loginConfig.setUserSalt(userSalt);
            BopLoginClient loginClient = new PasswordLoginClient(loginConfig);

            // 获取token, token相关说明请参考 文档中心 -> 用户授权说明
            BopLoginResponse loginResponse = loginClient.login();
            // 重载方法，可以传自定义请求唯一标识
            // BopLoginResponse loginResponse = loginClient.login(requestId);
            String token = loginResponse.getResponse().getAccessToken();


            SOutputinvoicePushRequest request = new SOutputinvoicePushRequest();
            request.setTaxNo(taxNo);//必填-销方税号
            request.setInvoiceCode(invoiceCode);//发票代码
            request.setInvoiceNo(invoiceNo);//发票号码
            request.setDigitInvoiceNo(digitInvoiceNo);//数电发票号码
            request.setPushPhone(pushPhone);//非必填-交付手机
            request.setPushEmail(pushEmail);//非必填-交付邮箱

            IBWClient client = new BWRestClient(url, appKey, appSecret); // 初始化一个客户端
            SOutputinvoicePushResponse response = client.sOutputinvoice().push(request, token);
            // 重载方法，可以传自定义请求唯一标识
            // SOutputinvoicePushResponse response = client.sOutputinvoice().push(request, token, requestId);
            System.out.println(response);
            return response;
        } catch (BWOpenException e) {
            throw e;
        }
    }
}